package com.egao.common.publicly.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.cms.entity.Article;
import com.egao.common.module.cms.service.ArticleService;
import com.egao.common.publicly.entity.ArticleVO;
import com.egao.common.publicly.mapper.PubliclyArticleMapper;
import com.egao.common.publicly.service.PubliclyArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 首页文章接口实现类
 *
 * @author pyx
 */
@Service
public class PubliclyArticleServiceImpl extends ServiceImpl<PubliclyArticleMapper, ArticleVO> implements PubliclyArticleService {
    @Autowired
    private ArticleService articleService;

    @Override
    public List<ArticleVO> listPage(PageParam<ArticleVO> pageParam) {
        if (pageParam.getInt("categoryId") == null) {
            return new ArrayList<>();
        }

        return baseMapper.listPage(pageParam);
    }

    @Override
    public ArticleVO getId(Integer id) {
        ArticleVO articleVO = baseMapper.getId(id);
        if (articleVO == null) {
            return new ArticleVO();
        }

        //更新点击量
        if (articleVO.getHits() == null) {
            articleVO.setHits(0);
        }
        Article article = new Article();
        article.setId(articleVO.getId());
        article.setHits(articleVO.getHits() + 1);
        articleService.updateById(article);
        return articleVO;
    }

    @Override
    public ArticleVO particular(Integer categoryId) {
        List<ArticleVO> listArticle = new ArrayList<>();
        listArticle = baseMapper.listParticular(categoryId);

        if (listArticle.size() == 0) {
            return new ArticleVO();
        }
        return listArticle.get(0);
    }

    @Override
    public List<ArticleVO> listNotice(PageParam<ArticleVO> pageParam) {
        if (pageParam.getInt("categoryId") == null) {
            return new ArrayList<>();
        }

        pageParam.setSize(6);

        return baseMapper.listNotice(pageParam);
    }

    @Override
    public List<ArticleVO> listImg(PageParam<ArticleVO> pageParam) {
        if (pageParam.getInt("categoryId") == null) {
            return new ArrayList<>();
        }

        pageParam.setSize(4);

        return baseMapper.listImg(pageParam);
    }

    @Override
    public Integer allHists(HashMap<String, String> createTimeMap, List<Integer> listCategoryId) {
        return baseMapper.allHists(createTimeMap, listCategoryId);
    }
}
